<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="流程名称" prop="noticeTitle">
        <el-input
            v-model="queryParams.definitionName"
            placeholder="请输入流程名称"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
        >新增</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="processDefinitionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="流程序号" align="center" prop="id" width="320" />
      <el-table-column label="流程标题" align="center" prop="name" width="440" />
      <el-table-column label="状态" align="center" prop="status" width="220">
        <template #default="scope">
           <span v-if="scope.row.isSuspended">
            <el-button type="danger" size="small">挂起</el-button>
          </span>
          <span v-if="!scope.row.isSuspended">
            <el-button type="success" size="small">已发布</el-button>
          </span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="deploymentTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.deploymentTime, '{y}-{m}-{d}:{h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <!--
          -->
          <el-button
              type="text"
              icon="Edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:notice:edit']"
          >修改</el-button>
          <el-button
              type="text"
              icon="Delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:notice:remove']"
          >删除</el-button>
          <el-button
              type="text"
              icon="Edit"
              @click="handleInfo(scope.row)"
          >查看</el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改流程对话框 -->
    <el-dialog :title="title" v-model="open" width="880px" append-to-body destroy-on-close>
      <div class="container" style="position:relative;height:750px;">
        <div class="canvas" ref="canvas"></div>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Definition">
import { ref, onMounted } from 'vue'
import { selProcessDefinitionList } from "@/api/camunda/definition";
import BpmnModeler from 'bpmn-js/lib/Modeler'
import BpmnViewer from "bpmn-js/lib/Viewer"
import router from '@/router/index.js'

const activeName = ref('first')
const { proxy } = getCurrentInstance();


const processDefinitionList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const department = ref(null);

let exportbtn = ref(null)
let bpmnModeler = ref(null);
let container = ref(null);
let canvas = ref(null);

/*
const viewer = new BpmnJS({
  container: department.value
});*/

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    definitionName: undefined
  },
  rules: {
    noticeTitle: [{ required: true, message: "流程标题不能为空", trigger: "blur" }],
    noticeType: [{ required: true, message: "流程类型不能为空", trigger: "change" }]
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询流程列表 */
function getList() {
  loading.value = true;
  selProcessDefinitionList(queryParams.value).then(response => {
    processDefinitionList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}
/** 表单重置 */
function reset() {
  form.value = {
    noticeId: undefined,
    noticeTitle: undefined,
    noticeType: undefined,
    noticeContent: undefined,
    status: "0"
  };
  proxy.resetForm("noticeRef");
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.noticeId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}
/** 新增按钮操作 */
function handleAdd(row) {
  router.push({ path: '/camunda/definition/model',query: { processId: row.id }})

  //router.push({ path: "/monitor/job-log/index", query: { jobId: row.id } });
}
/**修改按钮操作 */
function handleUpdate(row) {
  /*
  reset();
  const id = row.id || ids.value;
  getNotice(noticeId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改流程";
  });
   */
  console.log("-> row.id:"+ row.id);
  router.push({ path: '/camunda/definition/model',query: { id: row.id }})

}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["noticeRef"].validate(valid => {
    if (valid) {
      if (form.value.noticeId != undefined) {
        updateNotice(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addNotice(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}
/** 删除按钮操作 */
function handleDelete(row) {
  const noticeIds = row.noticeId || ids.value
  proxy.$modal.confirm('是否确认删除流程编号为"' + noticeIds + '"的数据项？').then(function() {
    return delNotice(noticeIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

function handleClick(){

}

onMounted(() => {


})

function handleInfo(row){
  open.value = true;


}

getList();
</script>

